from flask import Flask, jsonify
from flask_cors import CORS
import random
from datetime import datetime, timedelta

app = Flask(__name__)
CORS(app)  # 解决跨域问题

# 生成随机日期列表
def generate_dates(days=10):
    base = datetime.now()
    return [(base - timedelta(days=i)).strftime('%Y-%m-%d') for i in range(days-1, -1, -1)]

# 生成模拟股票数据
def generate_stock_data(dates, base_price=100, volatility=0.05):
    data = []
    price = base_price
    for date in dates:
        change = random.uniform(-volatility, volatility)
        price = round(price * (1 + change), 2)
        low = round(price * (1 - random.uniform(0, 0.02)), 2)
        high = round(price * (1 + random.uniform(0, 0.02)), 2)
        open_price = round(low + (high - low) * random.random(), 2)
        close = round(price, 2)
        data.append([date, open_price, close, low, high])
    return data

# 生成模拟成交量数据
def generate_volume_data(dates, base_volume=5000):
    return [[date, random.randint(int(base_volume*0.8), int(base_volume*1.2))] for date in dates]

# 生成模拟MACD数据
def generate_macd_data(dates):
    return [[date, random.uniform(-5, 5), random.uniform(-5, 5), random.uniform(-5, 5)] for date in dates]

# 生成模拟RSI数据
def generate_rsi_data(dates):
    return [[date, random.randint(30, 70)] for date in dates]

# 生成模拟KDJ数据
def generate_kdj_data(dates):
    return [[date, random.randint(20, 80), random.randint(20, 80), random.randint(20, 80)] for date in dates]

# 生成模拟OBV数据
def generate_obv_data(dates):
    obv = 10000
    data = []
    for date in dates:
        change = random.randint(-1000, 1000)
        obv += change
        data.append([date, obv])
    return data

# 生成模拟BOLL数据
def generate_boll_data(dates, base_price=100):
    data = []
    mid = base_price
    for date in dates:
        mid = round(mid * (1 + random.uniform(-0.03, 0.03)), 2)
        std = mid * 0.05
        upper = round(mid + 2 * std, 2)
        lower = round(mid - 2 * std, 2)
        data.append([date, lower, mid, upper])
    return data

# 生成模拟ATR数据
def generate_atr_data(dates):
    return [[date, random.uniform(1, 5)] for date in dates]

# 生成模拟DMI数据
def generate_dmi_data(dates):
    return [[date, random.uniform(10, 40), random.uniform(10, 40), random.uniform(10, 40)] for date in dates]

# 生成9个交易日数据
dates = generate_dates(9)

# K线数据接口
@app.route('/kline', methods=['GET'])
def get_kline_data():
    return jsonify(generate_stock_data(dates))

# 成交量数据接口
@app.route('/volume', methods=['GET'])
def get_volume_data():
    return jsonify(generate_volume_data(dates))

# MACD指标接口
@app.route('/macd', methods=['GET'])
def get_macd_data():
    return jsonify(generate_macd_data(dates))

# RSI指标接口
@app.route('/rsi', methods=['GET'])
def get_rsi_data():
    return jsonify(generate_rsi_data(dates))

# KDJ指标接口
@app.route('/kdj', methods=['GET'])
def get_kdj_data():
    return jsonify(generate_kdj_data(dates))

# OBV指标接口
@app.route('/obv', methods=['GET'])
def get_obv_data():
    return jsonify(generate_obv_data(dates))

# BOLL指标接口
@app.route('/boll', methods=['GET'])
def get_boll_data():
    return jsonify(generate_boll_data(dates))

# ATR指标接口
@app.route('/atr', methods=['GET'])
def get_atr_data():
    return jsonify(generate_atr_data(dates))

# DMI指标接口
@app.route('/dmi', methods=['GET'])
def get_dmi_data():
    return jsonify(generate_dmi_data(dates))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=10015, debug=True)